import { ExtensionContext, commands, window } from 'vscode';
import { programMenusProvider } from './ProgramMenusProvider';
import { twsManager } from './TwsManager';
import { webviewCommand } from './commands/WebviewCommand'
import * as businessCommand from './commands/BusinessCommand'
import { SIGIN, REFRESH_EXPLORER, CREATE_WEBVIEW, TO_PROGRAM_DETAIL, TO_TASK_DETAIL, TO_ASSIGNMENT_DETAIL, ADD_SINGLE_LANGUAGE_TEXT_DOC, SUBMIT_SINGLE_LANGUAGE_CODING, SUBMIT_SINGLE_LANGUAGE_CODING_WITH_TREE_BTN, TO_INDEX_PAGE } from './contstants/Commands';

export async function activate(context: ExtensionContext) {
    let panel: any;
    programMenusProvider.initialize(context)
    context.subscriptions.push(
        window.registerTreeDataProvider('twsExplorer', programMenusProvider),
        commands.registerCommand(SIGIN, async () => {
            if (await twsManager.login()) {
                panel.webview.postMessage({ type: 'LOGIN_INFO', params: { ...twsManager.getUser() } })
            } else {
                commands.executeCommand(TO_INDEX_PAGE)
            }
        }),
        commands.registerCommand(REFRESH_EXPLORER, () => programMenusProvider.refresh()),
        commands.registerCommand(CREATE_WEBVIEW, (routeData: any) => {
            panel = webviewCommand.createWebview(context, routeData)
        }),
        commands.registerCommand(TO_PROGRAM_DETAIL, (item: any) => {
            businessCommand.toProgramDetail(panel, item)
        }),
        commands.registerCommand(TO_TASK_DETAIL, (item: any) => {
            businessCommand.toTaskDetail(panel, item)
        }),
        commands.registerCommand(TO_ASSIGNMENT_DETAIL, (item: any) => {
            businessCommand.toAssignmentDetail(panel, item)
        }),
        commands.registerCommand(ADD_SINGLE_LANGUAGE_TEXT_DOC, (assignment: any) => {
            businessCommand.addConsoleLog(assignment)
        }),
        commands.registerCommand(SUBMIT_SINGLE_LANGUAGE_CODING, (userSubmission: any) => {
            businessCommand.submitSingleLanguageCodingQuiz(userSubmission)
        }),
        commands.registerCommand(SUBMIT_SINGLE_LANGUAGE_CODING_WITH_TREE_BTN, (userSubmission: any) => {
            businessCommand.submitSingleLanguageCodingWithTreeBtn(userSubmission)
        }),
        commands.registerCommand(TO_INDEX_PAGE, () => {
            businessCommand.toIndexPage(panel)
        }),
    )
    commands.executeCommand(TO_INDEX_PAGE)
}
export function deactivate() {
}